<?php
//积分操作

class Score{
   private $_error=null;
   private $_user=null;
	/**
	 +----------------------------------------------------------
	 * 积分操作
	 +----------------------------------------------------------
	 * @param string $action 类型
	 * @param string $score 配置积分
	 * @param string $reason　积分原因
	 +----------------------------------------------------------
	 * @return string
	 +----------------------------------------------------------
	 */
  static public function setscore($action=null,$score=0,$reason='',$user=null) {
        $actions=$step=$map=$_user=null;
         switch ($action) {
			case 'register':
                   $actions='注册';
                   $_user=$user;
				break;
		    case 'login':
		           //连续签到,
		           $_user=session('user_info');
  	               $map['uid']=$_user['id'];
		           $map['action']='签到';
		           $map['status']=1;
		           $logss=M('scores')->where($map)->field('action,add_time,step')->order('add_time desc')->limit(1)->select();
                   //找到上一天签到时间
                   $logs=$logss[0];
                   $today=mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                   if($logs['add_time']>$today)return 0;  //今天签到一次,则跳出
                   if(($logs['add_time']+3600*24)>=$today){  //昨天是否签到
                       if($logs['step']>=3){
                             $step=3;$score=3;
                       }else{
                             $step=$logs['step']+1;
                             $score=$step;
                       }
                        $reason='连续'.$step.'天签到';
                   }else{
                   	         $score=1;
                   	         $step=1;
                   	         $reason='每日签到';
                   }
                   session('step',$step);
                   $actions='签到';
		       break;
            case 'saidan':
                   $_user=session('user_info');
                   $actions='晒单';
                  break;
           case  'yao':
                   $actions='邀请好友';
                   if(!$user['buid'])return false;
			      $map['add_time']=array('gt'=>todaytime());
			      $map=array('ip'=>get_client_ip(),'uid'=>$user['buid'],'action'=>'邀请好友');
			      $t=M('scores')->where($map)->find();
                  if($t)return false;
                  $_user['id']=$user['buid'];
                  $_user['username']=$user['username'];
                  break;
		}
         if($score>0){
	         	 $status=1;
	         	 M('user_data')->where(array('uid'=>$_user['id']))->setInc('score',$score);
         }else{
	             $status=2;
	             $score=abs($score);
	             M('user_data')->where(array('uid'=>$_user['id']))->setDec('score',abs($score));
          }
            $dd=array(
                          'uid'=>$_user['id'],
                          'uname'=>$_user['username'],
                          'ip'=>get_client_ip(),
                          'action'=>$actions,
                          'desc'=>$reason,
		                  'score'=>$score,
		                  'add_time'=>time(),
		                  'status'=>$status,
		                  'step'=>$step,
   	      );
   	     $r=M('scores')->add($dd);
         return $score;
	}






}